package com.itqf.Controller;
/**
 * 大帅 2019/10/14 21:21
 */
import com.github.pagehelper.PageInfo;
import com.itqf.Enum.CodeEnum;
import com.itqf.Service.Requestion.RequestionService;
import com.itqf.Util.DTO.ResultDTO;
import com.itqf.Util.UpLoadUtil;
import com.itqf.Util.myPageInfo;
import com.itqf.Util.requestHeader.RequestHeaderUtil;
import com.itqf.entity.Manager;
import com.itqf.entity.Requestion;
import com.itqf.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class RequestionController {
   @Resource
   private RequestionService requestionService;

/** 
* @Description:  问题的模糊加分页
* @Param: [pageindex, requestiontitle] 
* @return: com.itqf.entity.Manager 
* @Author: 陈岗阳
* @Date: 2019/10/20 
*/ 
    @RequestMapping(method = RequestMethod.GET, value ="/huileme/a/m/RequestionController/interChange" )
    @ResponseBody
    public ResultDTO<PageInfo> findall(@RequestParam(defaultValue = "1") int pageindex, String requestiontitle){

        //模糊查询
       PageInfo<Requestion> findall = requestionService.findall(pageindex, myPageInfo.PAGESIZE,requestiontitle);
       ResultDTO resultDTO=new ResultDTO();
        if (!findall.getList().isEmpty())
        {
            resultDTO.setData(findall);
            resultDTO.setMessage(CodeEnum.selectsuccess.getMsg());
            resultDTO.setCode(CodeEnum.selectsuccess.getCode());
        }else
        {
            resultDTO.setData(findall);
            resultDTO.setMessage(CodeEnum.selecterror.getMsg());
            resultDTO.setCode(CodeEnum.selecterror.getCode());
        }
        return resultDTO;
    }


    /** 
    * @Description: 用户问题新增
    * @Param: [answerQuestions   问题的图片, request, answerTitle]
    * @return: com.itqf.entity.Manager 
    * @Author: 陈岗阳
    * @Date: 2019/10/20 
    */
    @ResponseBody
    @RequestMapping(value = "/huileme/a/u/RequestionController/addrequestion" ,method = RequestMethod.POST)
    public ResultDTO inserta(MultipartFile answerQuestions, HttpServletRequest request, String requestionTitle){

       ResultDTO resultDTO=new ResultDTO();

        if(answerQuestions==null){
            resultDTO.setMessage(CodeEnum.parameterincomplete.getMsg());
            resultDTO.setCode(CodeEnum.parameterincomplete.getCode());
            return  resultDTO;
        }
        //将文件上传到upload目录下
        String originalFilename = UpLoadUtil.getup(answerQuestions, request);

        //        调用获取请求头中的工具类
        RequestHeaderUtil requestHeaderUtil=new RequestHeaderUtil();
        final User getheader = requestHeaderUtil.getheader(request);
        final String userid1 = getheader.getUserid();


//        String userid="1";

        int i =requestionService.insertrequestion(originalFilename,userid1,requestionTitle);

        if (i<0){
            //没有查询到 返回200状态码

            resultDTO.setMessage(CodeEnum.addsuccess.getMsg());
            resultDTO.setCode(CodeEnum.addsuccess.getCode());
            return resultDTO;
        }else
        {
            resultDTO.setMessage(CodeEnum.addsuccess.getMsg());
            resultDTO.setCode(CodeEnum.addsuccess.getCode());

        }
             return resultDTO;
    }

    /** 
    * @Description: 提问删除
    * @Param: [requestionid] 
    * @return: com.itqf.entity.Manager 
    * @Author: 陈岗阳
    * @Date: 2019/10/20 
    */ 
    @RequestMapping(value = "/huileme/a/u/RequestionController/delete",method = RequestMethod.DELETE)
    @ResponseBody
    public ResultDTO delete(int requestionid,HttpServletRequest request){


        //        调用获取请求头中的工具类
        RequestHeaderUtil requestHeaderUtil=new RequestHeaderUtil();
        final User getheader = requestHeaderUtil.getheader(request);
        final String userid1 = getheader.getUserid();

//        String userid="1";
        final int delete = requestionService.delete(requestionid, userid1);
        ResultDTO resultDTO=new ResultDTO();

        if(delete<0){
            resultDTO.setMap(CodeEnum.deleteerror.getMsg());
            resultDTO.setCode(CodeEnum.deleteerror.getCode());
        }else
        {
            resultDTO.setMap(CodeEnum.deletesuccess.getMsg());
            resultDTO.setCode(CodeEnum.deletesuccess.getCode());
        }

        return resultDTO;
    }




    /**
     * @Description:  根据用户id和提问id 修改
     * @Param: [answerQuestions, request, answerTitle, requestionId]
     * @return: com.itqf.entity.Manager
     * @Author: 陈岗阳
     * @Date: 2019/10/20
     */
    @RequestMapping(method = RequestMethod.POST,value ="/huileme/a/u/RequestionController/update")
    @ResponseBody
    public ResultDTO updatea( MultipartFile answerQuestions, HttpServletRequest request,String answerTitle,@RequestParam(defaultValue = "-1") Integer requestionId){


        ResultDTO resultDTO=new ResultDTO();


        if(requestionId==-1&&answerQuestions==null)
        {
            resultDTO.setMessage(CodeEnum.parameterincomplete.getMsg());
            resultDTO.setCode(CodeEnum.parameterincomplete.getCode());
            return  resultDTO;
        }
        //将文件上传到upload目录下
        String originalFilename = UpLoadUtil.getup(answerQuestions, request);

//        //        调用获取请求头中的工具类
        RequestHeaderUtil requestHeaderUtil=new RequestHeaderUtil();
        final User getheader = requestHeaderUtil.getheader(request);
        final String userid1 = getheader.getUserid();


//        String userid="1";

        int i =requestionService.updateAnserer(originalFilename,userid1,answerTitle,requestionId);


        if (i<0){
            resultDTO.setMessage(CodeEnum.Updateerror.getMsg());
            resultDTO.setCode(CodeEnum.Updateerror.getCode());

        }
        else {
            resultDTO.setMessage(CodeEnum.Updatesuccess.getMsg());
            resultDTO.setCode(CodeEnum.Updatesuccess.getCode());

        }
        return resultDTO;

    }



}
